<div class="layui-hide" id='enian-nav-bar'>
 	<a data-url="">系统用户</a>
</div>

<div class="layui-btn-group enian-btn-group" >
	<button id='SadminList_add' class="layui-btn layui-btn-sm layui-btn-primary">
  		<i class="layui-icon">&#xe61f;</i><div class="enian-btn-group-text">添加用户</div>
  	</button>
  	<button id='search' class="layui-btn layui-btn-sm layui-btn-primary">
  		<i class="layui-icon">&#xe615;</i><div class="enian-btn-group-text">搜索</div>
  	</button>
</div>



<div id="adminList_data"></div>

<script type="text/html" id="appList_s_tool">
  <a class="layui-btn layui-btn-xs layui-btn-primary" title="用户组设置" lay-event="adminList_auth">权限</a>
  <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="adminList_del">删除</a>
</script>
  
  
<script>
layui.config({base: '/static/layui_extends/'})
layui.use(['table','form','laypage','jquery','element','enianTable'], function() {
	var table = layui.table,
	laypage = layui.laypage
	,$ = layui.jquery
	,enianTable = layui.enianTable
	,form = layui.form;
	var tableArg={where:[],order:[]};
	
	
    
    var fieldData = [
    // {"tableHead":{"type":"checkbox","fixed":"left"},"type":false}
    {"tableHead":{"field":"aid","width":80,"title":"ID","sort":true},"type":"noInput","typeData":{"msg":"不要输入汉字"}}
    ,{"tableHead":{"field":"username","minWidth":150,"title":"账号","edit":"text"},"type":"input","typeData":{"msg":"不要输入汉字"}}
    ,{"tableHead":{"field":"name","width":100,"title":"名称","edit":"text"},"type":"input","typeData":{"msg":"不要输入汉字"}}
    ,{"tableHead":{"field":"password","width":100,"title":"密码","event":"password"},"type":"input","typeData":{"msg":"请输入密码"}}
    ,{"tableHead":{"field":"note","width":100,"title":"备注","edit":"text"},"type":"input","typeData":{"msg":"请输入"}}
    ,{"tableHead":{"field":"status","width":100,"title":"状态"},"type":"switch","typeData":{"idName":"aid","on":{"data":1,"name":"启用"},"off":{"data":0,"name":"关闭"}}}
    ,{"tableHead":{"field":"start_time","width":170,sort:true,"title":"注册时间"},"type":"between","typeData":{"msg1":"请输入","msg2":"老铁"}}
    ,{"tableHead":{"field":"last_time","width":170,sort:true,"title":"上次登录"},"type":"time","typeData":{"msg1":"开始时间","msg2":"结束时间"}}
    ,{"tableHead":{"field":"last_ip","width":150,"title":"上次登录IP"},"type":"input","typeData":{"msg":"请输入"}}
    ,{"tableHead":{"width":130,"align":"center","fixed":"right","toolbar":"#appList_s_tool"},"type":false,"typeData":{"msg":"请输入"}}];
    var ets = enianTable.search(fieldData);
    layui.link('/static/layui_extends/enianTable/enianTable.css');
    
    
    
    
    
    var getTable = enianTable.table({
		elem:"#adminList_data"
		,url:"{:url('',['t'=>1])}"
		,data:fieldData
		,config:{
			limit:30
			,height:evar.tableHeight
			,toolbar:true
			// ,skin:'line'
			,page:true
			,autoSort: false
			,method:'post'
			//详见layui文档 数据表格 - 基本参数
		}
		,checkbox:function(field,obj){
			if(obj.elem.checked){
	   			ajaxFiled('field='+  field.name +'&value=1&aid=' + field.value)
		   	}else{
		   		ajaxFiled('field='+  field.name +'&value=0&aid=' + field.value)
		   	}
		}
		,toolEvent:function(obj){
			var data = obj.data;
			console.log();
		//删除数据
		if(obj.event === 'adminList_del') {
			layer.confirm('确定此条删除吗？删除后不可恢复？', function(index) {
					
						layer.close(index);
						//删除
						//return;
						var d_data = "aid="+data.aid;
						$.ajax({
								type: 'POST',
								url: "{:url("adminlist",["t"=>3])}",
								data: d_data,
								success: function(data) {
									//console.log(data);
									if(data.code == 1) {
										layer.msg('删除成功');
										obj.del();
									} else {
		
										layer.alert('删除失败，请刷新后重试');
									}
								}});
						});
			}else if(obj.event === 'password'){
		     	if(data.type==1){
		     		layer.msg('数据库模块类型不能编辑此项')
		     	}else{
		     		layer.prompt({
				         formType: 2
				         ,title: '修改 ID 为 ['+ data.aid +'] 的密码【请谨慎】'
				         ,value: ''
				       }, function(value, index){
				        layer.close(index);
				        var update_data="aid="+data.aid+"&field=password&value="+ value;
		   					ajaxFiled(update_data)
				         obj.update({
				           url: value
				         });
				       });
		     	}
	       
			}
			
				//权限按钮被单击
			if(obj.event === 'adminList_auth') {
				enianAdmin.go('{:url('setAdminAuthGroup')}?aid='+data.aid+'&name='+data.name)
			} 
		}
	})

    $('#SadminList_add').click(function(){
		enianAdmin.go('{:url("addAdmin")}')
    });
    
    $('#search').click(function(){
		ets.open();
    });

	//监听搜索
	ets.submit(function(d){
		getTable.renderObj().reload({
		  where:{where:d}
		  ,page: {
		    curr: 1 //重新从第 1 页开始
		  }
		});
	});
	
	//监听编辑
	getTable.on('edit',function(obj){
		var value = obj.value //得到修改后的值
    	,data = obj.data //得到所在行所有键值
   	 	,field = obj.field; //得到字段
   	 	var update_data="aid="+data.aid+"&field="+field+"&value="+ value;
 		ajaxFiled(update_data)
	})
	
	//监听排序
	getTable.on('sort',function(obj){
		tableArg.order= obj.field + " " + obj.type;
		table.reload(getTable.id, {
		    initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
		    ,where: tableArg
		});
	})


  	

	function ajaxFiled(d_data){
		$.ajax({
			type: 'POST',
			url: "{:url("adminList",["t"=>2])}",
			data: d_data,
			success: function(data) {
				if(data.code == 1) {
					layer.msg('修改成功');
				} else {
					var msg = data.msg?' 原因：'+data.msg:'请刷新后重试。';
					layer.alert('修改失败，'+msg);
				}
			}});
		
	}

});
</script>
